package com.logistics.inventory.controller;

import com.logistics.delivery.service.DeliveryService;
import com.logistics.inventory.service.InventoryService;
import com.logistics.pick.service.PickDetailService;
import com.logistics.qrcode.service.QrcodeService;
import com.logistics.utils.aspect.annotation.LoggerListener;
import com.logistics.utils.constants.Constants;
import com.logistics.utils.constants.LoggerMethod;
import com.logistics.utils.exception.MessageException;
import com.logistics.utils.response.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("lgs/processOutput")
public class ProcessOutputController {

    @Autowired
    private QrcodeService qrcodeService;
    @Autowired
    private PickDetailService pickDetailService;
    @Autowired
    private InventoryService inventoryService;
    @Autowired
    private DeliveryService deliveryService;

    @GetMapping("findStationPickList/{code}")
    @LoggerListener(businessName = "查询站点物料", methodName = LoggerMethod.SEARCH)
    public ResponseEntity findStationPickList(@PathVariable("code") String station) throws MessageException {
        String value = qrcodeService.valueByCode(station, Constants.CODE_TYPE_LOCATION);
        return ResponseUtil.success(pickDetailService.findStationPickList(value));
    }

    @GetMapping("checkStationContainer/{station}")
    @LoggerListener(businessName = "查询站点是否有绑定的托盘", methodName = LoggerMethod.SEARCH)
    public ResponseEntity checkStationContainer(@PathVariable("station") String station) throws MessageException {
        return ResponseUtil.success(inventoryService.checkStationContainer(station));
    }

    @GetMapping("findStationInventory/{station}")
    @LoggerListener(businessName = "查询站点托盘,来料配送物料信息", methodName = LoggerMethod.SEARCH)
    public ResponseEntity findStationInventory(@PathVariable("station") String station) throws MessageException {
        return ResponseUtil.success(inventoryService.findStationInventory(station));
    }

    @GetMapping("clearContainerInventory/{container}")
    @LoggerListener(businessName = "清空托盘物料信息", methodName = LoggerMethod.SEARCH)
    public ResponseEntity clearContainerInventory(@PathVariable("container") String container) throws MessageException {
        inventoryService.deleteStationOrContainer(container,"0");
        return ResponseUtil.success();
    }

    @PutMapping("startDelivery")
    @LoggerListener(businessName = "工序产出开始配送",methodName = LoggerMethod.SAVE)
    public ResponseEntity startDelivery(@RequestBody String pickId) throws MessageException {
        deliveryService.checkDelivery(pickId);
        deliveryService.createDelivery(pickId);
        return ResponseUtil.success();
    }
}
